package com.imooc.ecommerce.controller;

import com.alibaba.fastjson.JSON;
import com.imooc.ecommerce.rocket.RocketMQProducer;
import com.imooc.ecommerce.vo.QinyiMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * <p>SpringBoot 集成 RocketMQ
 *
 * @author Yaxi.Zhang
 * @since 2022/11/15 18:54
 */
@Slf4j
@RestController
@RequestMapping("/rocket-mq")
public class RocketMQController {

    private final RocketMQProducer rocketMQProducer;
    private final AtomicInteger ai = new AtomicInteger(0);

    public RocketMQController(RocketMQProducer rocketMQProducer) {
        this.rocketMQProducer = rocketMQProducer;
    }

    @GetMapping("/message-with-value")
    public void sendMessageWithValue() {
        rocketMQProducer.sendMessageWithValue(JSON.toJSONString(getRocketMQMessage()));
    }

    @GetMapping("/message-with-key")
    public void sendMessageWithKey() {
        rocketMQProducer.sendMessageWithKey("Qinyi", JSON.toJSONString(getRocketMQMessage()));
    }

    @GetMapping("/message-with-tag")
    public void sendMessageWithTag() {
        rocketMQProducer.sendMessageWithTag("qinyi", JSON.toJSONString(getRocketMQMessage()));
    }

    @GetMapping("/message-with-all")
    public void sendMessageWithAll() {
        rocketMQProducer.sendMessageWithAll("Qinyi", "qinyi", JSON.toJSONString(getRocketMQMessage()));
    }

    public QinyiMessage getRocketMQMessage() {
        Integer count = ai.incrementAndGet();
        return new QinyiMessage(count, "Qinyi-Study-RocketMQ-In-SpringBoot" + count);
    }

}
